using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {

	CameraScript _Camera;
	Rope _rope;
	BackGround _Br;
	public Basket _bask;
	GetInput _Input;
	public Bird _bird;
	Bullet _bullet;
	Score _score;
	
	public enum State {
		Menu,
		Game,
		Lose,
		None
	}	
	State SCR;

	void Start () {
		SetFrameRate ();
		LoadCamera ();
		LoadBackground ();
		LoadRope ();
		LoadBasket ();
		LoadInput ();
		LoadBird ();
		LoadScore ();
		LoadHD ();
		Begin ();
	}
	

	void Update () {
		switch (SCR){
			case State.Menu:
				SetHD();
				_Input.Update(SCR);
				break;
			case State.Game:
				CheckLose();
				_Input.Update(SCR);
				_bird.SetVanToc();
				_bird.Update();
				_score.UpdateScore();
				break;
			case State.Lose:
				_Input.Update(SCR);
				break;
		}
	}

	void Begin()
	{
		SCR = State.Menu;
	}

	public void StartGame()
	{
		SCR = State.Game;
		_score.InitScore ();
		SetHD ();
	}
	void SetFrameRate(){
		Application.targetFrameRate = 30;
	}

	void CheckLose()
	{
		if(Score.vuottrung ==3 || Score.lose ==3)
			EndGame();
	}

	void EndGame()
	{
		SCR = State.Lose;
		SetHD ();
	}

	public void ResetGame()
	{
		SCR = State.Menu;
		_bask.StartGame ();
		_bird.ResetGame ();
		SetHD ();

	}
	
	void LoadCamera(){
		_Camera = new CameraScript ();
		_Camera.Load ();
	}

	void LoadBackground()
	{
		_Br = new BackGround ();
		_Br.Load ();
	}

	void LoadRope()
	{
		_rope = new Rope ();
		_rope.Load ();
	}
	void LoadBasket()
	{
		_bask = new Basket ();
		_bask.Load ();
	}

	void LoadInput()
	{
		_Input = new GetInput ();
		_Input.Load ();
	}

	void LoadBird()
	{
		_bird = new Bird ();
		_bird.Load ();
	}

	void LoadScore()
	{
		_score = new Score ();
		_score.LoadScore ();
	}

	GameObject Huongdan;
	void LoadHD()
	{
		Huongdan = Graphic.Draw ("Huongdan", new Vector2 (1080, 760), "IMG/huongdan");
		Huongdan.transform.position = new Vector3 (0, 1600, 0);
	}

	void SetHD()
	{
		if(SCR == State.Game)
			Huongdan.transform.position = new Vector3 (0, 1600, 0);
		if(SCR == State.Menu || SCR == State.Lose)
			Huongdan.transform.position = new Vector3 (0, 0, 0);
	}
}
